using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Tools;

namespace ToolWindows
{
    public partial class Brokers : UserControl, IRefreshData
    {
        public Brokers()
        {
            InitializeComponent();
        }

        protected string connectionString = string.Empty;

        public string ConnectionString
        {
            set
            {
                connectionString = value;
            }
        }

        public void RefreshData()
        {
            string connectionString = UtilitySqlTools.Current.ConnectionString();

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                try
                {
                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine("SELECT   name AS [Database Name],");
                    sb.AppendLine("         Upper(service_broker_guid) AS GUID,");
                    sb.AppendLine("         is_broker_enabled [Is broker enabled?]");
                    sb.AppendLine("FROM sys.databases");

                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = new SqlCommand();
                    da.SelectCommand.CommandText = sb.ToString();
                    da.SelectCommand.CommandTimeout = 0;
                    da.SelectCommand.CommandType = CommandType.Text;
                    da.SelectCommand.Connection = connection;

                    DataSet ds = new DataSet();
                    da.Fill(ds);

                    dgBrokers.DataSource = ds.Tables[0];
                    dgBrokers.Columns["Is broker enabled?"].ReadOnly = false;
                }
                catch (Exception exception)
                {
                    // Logger
                }

                connection.Close();
            }
        }

        private void dgBrokers_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex > 0)
            {
                if (dgBrokers.Rows[e.RowIndex].Cells[e.ColumnIndex].OwningColumn.Name.ToUpper() == "IS BROKER ENABLED?")
                {
                    string database = dgBrokers.Rows[e.RowIndex].Cells["Database Name"].Value.ToString();

                    bool is_checked = (bool)dgBrokers.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;

                    try
                    {
                        string connectionString = UtilitySqlTools.Current.ConnectionString();

                        using (SqlConnection connection = new SqlConnection(connectionString))
                        {
                            UtilitySqlTools.EnableDisableBroker(database, connection, !is_checked);

                            connection.Close();
                        }

                        dgBrokers.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = !is_checked;
                    }
                    catch (Exception exception)
                    {
                        // Logger
                    }
                }
            }
        }

        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RefreshData();
        }
    }
}
